package com.clms.rents.service.impl;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clms.common.R;
import com.clms.rents.mapper.TabCarShop;
import com.clms.rents.pojo.entity.BaseArea;
import com.clms.rents.pojo.entity.TbShop;
import com.clms.rents.pojo.vo.CarShopVo;
import com.clms.rents.service.CarShopService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: clms-third
 * @description: 4s店业务层
 * @author: 杨浩
 * @create: 2021-07-22 12:07
 **/

@Service
public class CarShopServiceimpl implements CarShopService {

    @Resource
    private TabCarShop tabCarShop;


    public R selectall() {
        List<TbShop> tbShops = tabCarShop.selectList(null);
        return R.ok().put("allCarShops",tbShops);

    }

    @Override
    public R pageSelect(Integer currentPage, Integer pageSize, CarShopVo carShopVo) {
        Page<TbShop> page=new Page<>(currentPage,pageSize);
        QueryWrapper<TbShop> queryWrapper=new QueryWrapper<>();
        if(StringUtils.isNotEmpty(carShopVo.getShopname())){
            queryWrapper.like("shopname",carShopVo.getShopname());
        }
        Page<TbShop> page1=tabCarShop.selectPage(page,queryWrapper);
        return R.ok().put("total",page1.getTotal()).put("records",page1.getRecords());
    }

    @Override
    public R insert(TbShop tbShop) {
        int i=tabCarShop.insert(tbShop);
        return R.ok();
    }

    @Override
    public R update(TbShop tbShop) {
        int i=tabCarShop.updateById(tbShop);
        return R.ok();
    }

    @Override
    public R selectById(Integer id) {
        TbShop tbShop = tabCarShop.selectById(id);
        return R.ok().put("huixian",tbShop);
    }



    @Override
    public R delete(Integer id) {
        int i=tabCarShop.deleteById(id);
        return R.ok();
    }

    @Override
    public R shengShiXian() {
        //查询所有
        List<BaseArea> allList=tabCarShop.selectall();

         List<BaseArea> sheng=new ArrayList<>();
              for(BaseArea baseArea:allList){
                  if(baseArea.getParentid()==0){
                      sheng.add(baseArea);
                  }
              }

              for(BaseArea baseArea:sheng){
                  List<BaseArea> baseAreas=tabCarShop.shengShixian(baseArea.getBaseAreaid());
                  for(BaseArea baseArea1:baseAreas){
                      List<BaseArea> xian=tabCarShop.shengShixian(baseArea1.getBaseAreaid());
                      baseArea1.setChildren(xian);
                  }
                  baseArea.setChildren(baseAreas);
              }
        return R.ok().put("sheng",sheng);

    }

    private List<BaseArea> findchildren(List<BaseArea> menus,Long pid){
        //声明一个集合容器存放孩子节点
        List<BaseArea> children=new ArrayList<>();
        for(BaseArea child:menus){
            if(child.getParentid().equals(pid)){
                children.add(child);
            }
        }
        //查询孩子节点下的孩子节点
        for(BaseArea c:children){
            c.setChildren(findchildren(menus,c.getBaseAreaid()));
        }
        return children;
    }
}
